<?php namespace testsacvoyage\storage;

use \ArrayIterator;

require_once 'AbstractTest.php';

require_once 'sacvoyage/storage/Gateway.php';
use sacvoyage\storage\Gateway;

require_once 'sacvoyage/storage/Table.php';
use sacvoyage\storage\Table;

require_once 'sacvoyage/storage/Sorter.php';
use sacvoyage\storage\Sorter;

require_once 'sacvoyage/Object.php';
use sacvoyage\Object;




class SorterTest extends AbstractTest {

	public function setUp() {
		$this->resetDatabase();	
		$this->fillDatabase();
	}

	/*
	 * 
	 */
	public function testSortPassedKeys() {
		$expected=array(
			new Object(array('_id'=>'newidea', '_rev'=>'1')),
			new Object(array('_id'=>'newnote', '_rev'=>'1')),
			new Object(array('_id'=>'newtask', '_rev'=>'2')),
		
		);
		
		$documents=$this->sortDocuments(
			new ArrayIterator(array(
				new Object(array('_id'=>'newtask', '_rev'=>'2')),
				new Object(array('_id'=>'newnote', '_rev'=>'1')),	
				new Object(array('_id'=>'newidea', '_rev'=>'1'))
			)),
			array(
				new Object(array('priority'=>'asc'))
			)
		);
		
		$this->assertEquals($expected, $documents); 
	}

	/*
	 * 
	 */
	public function testSortAll() {
		$expected=array(
			new Object(array('_id'=>'newidea', '_rev'=>'1')), // no priority
			new Object(array('_id'=>'newbook', '_rev'=>'1')), // no priority

			new Object(array('_id'=>'newnote', '_rev'=>'1')), // 4

			new Object(array('_id'=>'newtask', '_rev'=>'1')), // Купть Б.П. БНО, он будет рад
			new Object(array('_id'=>'newtask', '_rev'=>'3')), // Купить Б.П. БНО, он будет рад. Не забыть ...
			new Object(array('_id'=>'newtask', '_rev'=>'2')), // Купить Б.П. БНО, он будет рад		
			new Object(array('_id'=>'newnote', '_rev'=>'2'))  // Купить батон и хлеб. Не забыть укроп
		
		);
		
		$documents=$this->sortDocuments(
			null,
			array(
				new Object(array(
					'priority'=>'asc', 
				)),
				new Object(array(
					'text'=>'desc', 
				)),
			)
		);

		$this->assertEquals($expected, $documents); 
	}

	/*
	 * 
	 */
	public function testAllWithoutSorting() {
		$expected=array(
			new Object(array('_id'=>'newnote', '_rev'=>'1')),
			new Object(array('_id'=>'newbook', '_rev'=>'1')),
			new Object(array('_id'=>'newidea', '_rev'=>'1')),
			new Object(array('_id'=>'newnote', '_rev'=>'2')),
			new Object(array('_id'=>'newtask', '_rev'=>'1')),
			new Object(array('_id'=>'newtask', '_rev'=>'2')),
			new Object(array('_id'=>'newtask', '_rev'=>'3'))
		);
		
		$documents=$this->sortDocuments(
			null,
			null
		);
		
		$this->assertEquals($expected, $documents); 
	}

	/*
	 * 
	 */
	public function testDoNothing() {
		$expected=array(
			new Object(array('_id'=>'newnote', '_rev'=>'1')),
			new Object(array('_id'=>'newbook', '_rev'=>'1')),
			new Object(array('_id'=>'newnote', '_rev'=>'2')),
			new Object(array('_id'=>'newtask', '_rev'=>'3'))
		);
		
		$documents=$this->sortDocuments(
			new ArrayIterator($expected),
			null
		);
		
		$this->assertEquals($expected, $documents); 
	}



	
	public function sortDocuments($keyIterator=null, $order=null) {
		return iterator_to_array( 
			new Sorter(
				new Table($this->getGateway(), 'alex_db_notes'),
				$keyIterator,
				$order
			)
		);
	}

}

?>
